package com.sdu.douban_comment.controller;

import com.sdu.douban_comment.entity.Film;
import com.sdu.douban_comment.entity.RequestParam;
import com.sdu.douban_comment.entity.Result;
import com.sdu.douban_comment.service.CommentSerivce;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@RestController
public class CommentController {

    private final static Logger logger=LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private CommentSerivce service;

    @GetMapping("/test")
    public String hello(){
        return "hello!!";
    }


    @GetMapping("/count")
    public Result<Long> count(){
        long count = service.count();
        return Result.ok("查询成功",count);
    }


    @GetMapping("/importData")
    public String importData(){
        logger.info("开始导入数据");
        try {
            int count = service.importData();
            logger.info("导入数据条数为：{}",count);
        } catch (IOException e) {
            logger.error("导入数据发生错误",e);
            return "导入数据失败";
        }
        return "导入数据成功";
    }

    /*
    * 展示电影评分的平均分、中位数、最高分、最低分，以数据集中“评分”属性为依据进行展示
    * */
    @GetMapping("/score/{model}")
    public Result<Double> score(@PathVariable("model") int modle){
        logger.info("评分查询模式为:{}",modle);
        int code=200;
        double result=0;
        try{
            switch (modle){
                case 1:
                    result=service.findScoreMax();
                    break;
                case 2:
                    result=service.findScoreMin();
                    break;
                case 3:
                    result=service.findScoreMed();
                    break;
                case 4:
                    result=service.findScoreAvg();
                    break;
                default:
                    code=500;
                    break;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return Result.error(e.getMessage());
        }

        if(code==500){
            return Result.error("输入参数有误;1-Max,2-Min,3-Med,4-Avg");
        }
        return Result.ok("查询成功",result);
    }



    /*
    * 按日期统计电影数量-以上映日期为准-分为两个模式
    * model: 1-按年份统计
    *        2-按年+月份统计
    * */
    @GetMapping("/count/date/{model}/{date}")
    public Result<Map<String,Integer>> countFilm(@PathVariable("model") int model,@PathVariable("date") String date){
        if(model!=1&&model!=2){
            return Result.error("只接受按年或者月统计,1-按年份统计；2-按月份统计");
        }
        Map<String, Integer> result=null;
        if(model==1){
            result=service.countFilmByYear();
        }else {
            if(date.length()!=4){
                return Result.error("按提供正确的年份");
            }
            /*String pattern = "\\d{4}-\\d{2}";
            Pattern regexPattern = Pattern.compile(pattern);
            Matcher matcher = regexPattern.matcher(date);
            if(!matcher.matches()){
                logger.warn("传入参数有误, date:{}",date);
                return Result.error("传入时间格式有误");
            }*/
            result=service.countFilmByMonth(date);

        }
        return Result.ok("查询成功",result);
    }

    /*
    * 选择你关注的两个国家或地区，按照数据集中“类型”属性展示电影数量分布；
    * */
    @PostMapping("/count/type")
    public Result<Map<String, Integer>> coundType(@RequestBody String[] locations ){
        if(locations==null||locations.length<1){
            return Result.error("请选择感兴趣的地区");
        }
        Map<String, Integer> result = service.countFileByType(locations);

        return Result.ok("查询成功",result);
    }

    @PostMapping("/count/city")
    public Result<Map<String, Integer>> coundCity(@RequestBody String[] types ){

        Map<String, Integer> result = service.countFileByCity(types);

        return Result.ok("查询成功",result);
    }

    @GetMapping("/show/types")
    public Result<String[]> showTypes(){

        String[] types=service.showTypes();

        return Result.ok("查询成功",types);
    }

    @GetMapping("/show/city")
    public Result<String[]> showProductCty(){

        String[] cities=service.showProductCty();

        return Result.ok("查询成功",cities);
    }

    @GetMapping("/show/hotActors")
    public Result<Map<String, Integer>> hotActors(){
        Map<String, Integer> result=service.showHotActors();

        return Result.ok("查询成功",result);
    }


    @PostMapping("/show/films")
    public Result<Page<Film>> showFilms(@RequestBody RequestParam request){
        if(request.getOrderBy()==null){
            //默认按评分排序
            request.setOrderBy(0);
        }else if(request.getOrderBy()!=0&&request.getOrderBy()!=1){
            return Result.error("请选择按时间还是评分排序");
        }
        Page<Film> result=service.showFilms(request);

        return Result.ok("查询成功",result);
    }




}
